package com.pay.wx;

import common.ClientUtils;
import common.WXPayConstants;
import common.WXPayUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付~~委托扣款模式
 *
 * @author chen
 */
public class WithoutCodePay {

    private WXConfig config;

    public WithoutCodePay(WXConfig wxConfig) throws Exception {
        this.config = wxConfig;
        WXPayUtils.checkWXPayConfig(config);
    }

    /**
     * 小程序纯签
     *
     * @param plan_id
     * @param name
     * @param user_code
     * @param request_serial
     * @param notify_ui
     * @return
     * @throws Exception
     */
    public Map<String, String> OBJECT(String plan_id, String notify_ui, String name, String user_code, String request_serial) throws Exception {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("mch_id", config.getMchID());
        paramMap.put("appid", config.getAppID());
        paramMap.put("plan_id", plan_id);
        paramMap.put("contract_code", user_code);
        paramMap.put("request_serial", request_serial);
        paramMap.put("contract_display_account", name);
        paramMap.put("notify_url", notify_ui);
        paramMap.put("timestamp", System.currentTimeMillis() + "");
        String sign = WXPayUtils.generateSignature(paramMap, config.getKey());
        paramMap.put("sign", sign);
        return paramMap;
    }

    /**
     * 查询签约情况
     *
     * @param contract_id
     * @param plan_id
     * @return
     * @throws Exception
     */
    public Map<String, String> querycontract(Long contract_id, Integer plan_id) throws Exception {
        //查询签约情况
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("version", "1.0");
        param.put("contract_id", contract_id.toString());
        param.put("plan_id", plan_id.toString());
        String sign = WXPayUtils.generateSignature(param, config.getKey());
        param.put("sign", sign);
        String paramXml = WXPayUtils.mapToXml(param);
        String resultMap = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/papay/querycontract", paramXml, "UTF-8");
        System.out.println(resultMap);
        return WXPayUtils.xmlToMap(resultMap);
    }

    /**
     * 委托代扣扣款申请
     *
     * @param body
     * @param out_trade_no
     * @param total_fee
     * @param notify_url
     * @param contract_id
     * @return
     * @throws Exception
     */
    public Map<String, String> pappayapply(String body, String out_trade_no, Integer total_fee, String notify_url, String contract_id) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("nonce_str", WXPayUtils.generateNonceStr());
        param.put("body", body);
        param.put("out_trade_no", out_trade_no);
        param.put("total_fee", total_fee.toString());
        param.put("spbill_create_ip", "127.0.0.1");
        param.put("notify_url",  notify_url);
        param.put("trade_type", "PAP");
        param.put("contract_id", contract_id);
        String sign = WXPayUtils.generateSignature(param, config.getKey());
        param.put("sign", sign);
        String paramXml = WXPayUtils.mapToXml(param);
        String resultXml = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/pay/pappayapply", paramXml, "UTF-8");
        return WXPayUtils.xmlToMap(resultXml);
    }

    /**
     * 免密支付订单查询
     *
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    public Map<String, String> paporderquery(String out_trade_no) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("out_trade_no", out_trade_no);
        param.put("nonce_str", WXPayUtils.generateNonceStr());
        String signature = WXPayUtils.generateSignature(param, config.getKey());
        param.put("sign", signature);
        String paramXml = WXPayUtils.mapToXml(param);
        String resultXml = ClientUtils.doPostMethod("https://api.mch.weixin.qq.com/pay/paporderquery", paramXml, "UTF-8");
        return WXPayUtils.xmlToMap(resultXml);
    }

    /**
     * 委托代扣支付退款
     *
     * @param body
     * @param out_trade_no
     * @param out_refund_no
     * @param total_fee
     * @param refund_fee
     * @param notify_url
     * @param contract_id
     * @return
     * @throws Exception
     */
    public Map<String, String> pappayrefund(String body, String out_trade_no, String out_refund_no, Integer total_fee, Integer refund_fee, String notify_url, String contract_id) throws Exception {
        Map<String, String> param = new HashMap<>();
        param.put("appid", config.getAppID());
        param.put("mch_id", config.getMchID());
        param.put("nonce_str", WXPayUtils.generateNonceStr());
        param.put("body", body);
        param.put("out_trade_no", out_trade_no);
        param.put("out_refund_no", out_refund_no);
        param.put("total_fee", total_fee.toString());
        param.put("refund_fee", refund_fee.toString());
        param.put("spbill_create_ip", "127.0.0.1");
        param.put("notify_url",  notify_url);
        param.put("trade_type", "PAP");
        param.put("contract_id", contract_id);
        String sign = WXPayUtils.generateSignature(param, config.getKey());
        param.put("sign", sign);
        String paramXml = WXPayUtils.mapToXml(param);
        Map<String, String> map = ClientUtils.certPost("https://api.mch.weixin.qq.com/secapi/pay/refund", paramXml, config.getMchID(), config.getCertStream());
        return map;
    }
}
